package com.carsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.carsystem.entity.Car;
import com.carsystem.entity.Openrend;
import com.carsystem.entity.Order;
import com.carsystem.entity.Shop;
import com.carsystem.mapper.CarMapper;
import com.carsystem.mapper.OpenrendMapper;
import com.carsystem.mapper.OrderMapper;
import com.carsystem.mapper.ShopMapper;
import com.carsystem.service.IShopService;
import com.carsystem.utils.MessageConstant;
import com.carsystem.utils.PageResult;
import com.carsystem.utils.QueryPageBean;
import com.carsystem.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ykl
 * @since 2021-12-04
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {

    @Resource
    private ShopMapper shopMapper;
    @Resource
    private CarMapper carMapper;
    @Resource
    private OpenrendMapper openrendMapper;
    @Resource
    private OrderMapper orderMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Shop> shopPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryPageBean.getQueryString())){
            shopQueryWrapper.like("shop_name",queryPageBean.getQueryString()).or().like("phone",queryPageBean.getQueryString());
        }
        Page<Shop> shopPage1 = shopMapper.selectPage(shopPage, shopQueryWrapper);
        PageResult pageResult = new PageResult(shopPage1.getTotal(), shopPage1.getRecords());
        return pageResult;
    }

    @Override
    public Result addShop(Shop shop) {
        int insert = shopMapper.insert(shop);
        if (insert==1){
            return  new Result(true, MessageConstant.ADD_SHOP_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_SHOP_FAIL);
    }

    @Override
    public Result updateShop(Shop shop) {
        int i = shopMapper.updateById(shop);
        if (i==1){
            return  new Result(true, MessageConstant.EDIT_SHOP_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_SHOP_FAIL);
    }

    @Override
    public Result deleteShop(int shopId) {
        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
        carQueryWrapper.eq("shop_id",shopId);
        carMapper.delete(carQueryWrapper);
        QueryWrapper<Openrend> openrendQueryWrapper= new QueryWrapper<>();
        openrendQueryWrapper.eq("shop_id",shopId);
        openrendMapper.delete(openrendQueryWrapper);
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("shop_id",shopId);
        orderMapper.delete(orderQueryWrapper);
        int i = shopMapper.deleteById(shopId);
        if (i==1){
            return  new Result(true, MessageConstant.DELETE_SHOP_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_SHOP_FAIL);
    }

    @Override
    public Result findAllShop() {
        List<Shop> shopList=null;
        try {
             shopList = shopMapper.selectList(null);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_SHOP_FAIL);
        }
        return  new Result(true, MessageConstant.QUERY_SHOP_SUCCESS,shopList);
    }
}
